<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2015/8/11
 * Time: 15:01
 */

namespace Home\Controller;

use Think\Controller;
use Think\Log;

class AccountController extends Controller
{

    /**
     * 个人中心首页
     */
    public function index()
    {
        $memberModel = M("wx_member_info","jt_") -> where("openid='%s'",getOpenId()) -> field("nickname,headimgurl") -> find();
        $this -> assign("memberModel",$memberModel);
        $this->display();
    }

    /**
     * 乘客
     */
    public function passenger()
    {
        //把openId保存到session，给modify和delete用
        if(!isset($_SESSION['openId'])){
            session("openId",getOpenId());
        }
        $buyerInfoTable = M("buyer_info", "jt_");
        $buyerList = $buyerInfoTable->where("openId='%s'",session("openId"))->order('create_time DESC')->select();
        $this->assign("buyerlist", $buyerList);
        $this->display();
    }

    /**
     * 帮助文档
     */
    public function help()
    {
        $this->display();
    }

    /**
     * 积分规则
     */
    public function point()
    {
        $this->display();
    }

    /**
     * ajax
     * modify buyer info
     */
    public function modify()
    {
        //data to be saved
        $data['name'] = I("name");
        $data['phone'] = I("phone");
        $data['identity'] = I("identity");

        $buyerInfoTable = M("buyer_info", "jt_");
        $result = $buyerInfoTable->where("id='%s' and openId='%s'",I("id"),session("openId"))->save($data);
        if (!$result) {
            $json['status'] = 0;
            $json['msg'] = "修改失败,不存在该数据!";
        } else {
            $json['status'] = 1;
            $json['msg'] = "修改成功!";
            $json['new'] = $data;
        }
        exit(json_encode($json));
    }
    /**
     * ajax
     * 删除取票人记录
     */
    public function delete(){
        //Log::write(getOpenId());
        $buyerInfoTable  = M("buyer_info", "jt_");
        $result = $buyerInfoTable ->where("id='%s' and openId='%s'",I("id"),session("openId"))-> delete();
        if(!$result){
            $json['status'] = 0;
            $json['msg'] = "删除失败,不存在该数据!";
        } else{
            $json['status'] = 1;
            $json['msg'] = "删除成功!";
        }
        exit(json_encode($json));
    }

    /**ajax
     * 添加取票人记录
     */
    public function add(){
        $buyerTable  = M("buyer_info", "jt_");
        //保存元
        $data['id'] = getUUIDShort();
        $data['name'] = I("name");
        $data['phone'] = I("phone");
        $data['identity'] = I("identity");
        $data['openId'] = session("openId");
        $data['create_time'] = array("exp","now()");
        //判断是否已存在
        $result = $buyerTable -> where("name='%s' and phone='%s' and identity='%s'",$data['name'],$data['phone'],$data['identity']) -> find();
        if($result){
            $this -> ajaxReturn(array("status"=>"fail","msg"=>"该取票人信息记录已存在!"));
        }
        //保存
        $saveResult = $buyerTable -> add($data);
        //成功返回该记录
        if($saveResult){
            $retArray = array(
                "status"=>"success",
                "id"=>$data['id'],
                "name"=>$data['name'],
                "phone"=>$data['phone'],
                "identity"=>$data['identity']
            );
            Log::write(json_encode($retArray));
            $this -> ajaxReturn($retArray);
        } else{
            $this -> ajaxReturn(array("status"=>"fail","msg"=>"添加出错了!"));
        }
    }
}